import java.util.Arrays;
import java.util.Scanner;
import java.util.TooManyListenersException;

public class Test05 {
    /*public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int Q = in.nextInt();
        long minK = in.nextLong() , maxK = in.nextLong();
        for(int i = 0 ; i  < Q; i++){
            long m = in.nextLong(); long d = in.nextLong();
            //zui xiaode
            minK = Math.min(minK, m-d);
            //zuida
            maxK = Math.max(maxK , m+d);
        }
        if(minK <= maxK) System.out.println(maxK);
        else System.out.println(-1);
    }*/

    /*public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt(); int m = in.nextInt();
        int[][] arr = new int[n][m];
        int[] maxFront = new int[m]; int[] maxLeft = new int[n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m;j++){
                arr[i][j] = in.nextInt();
                if(arr[i][j] > maxFront[j]) maxFront[j] = arr[i][j];
                if(arr[i][j] > maxLeft[i]) maxLeft[i] = arr[i][j];

            }
        }

        int frontP = 0;
        for(int max:maxFront) frontP+=max;
        int leftP = 0;
        for(int max: maxLeft) leftP += max;
        int totalP = 0;
        for(int j = 0 ; j < m; j++){
            int columMax = 0;
            for(int i = 0; i < n; i++){
                columMax  = Math.max(columMax, arr[i][j]);
            }
            totalP += columMax;
        }
        System.out.println(frontP+" "+leftP+" "+totalP);
    }*/

    static class UnionFind{
        int[] parent;
        int[] rank;

        public UnionFind(int n){
            parent = new int[n+1];
            rank = new int[n+1];
            for (int i = 0; i <= n; i++) {
                parent[i] = i;
            }
        }
        public int find(int x){
            if(parent[x] != x){
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }

        public void union(int x, int y){
            int rootX = find(x);
            int rootY = find(y);
            if(rootX != rootY){
                if(rank[rootX] < rank[rootY]){
                    parent[rootX] = rootY;
                }else if(rank[rootX] > rank[rootY]){
                    parent[rootY] = rootX;
                }else{
                    parent[rootY] = rootX;
                    rank[rootX]++;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        int m = in.nextInt();
        UnionFind unionFind = new UnionFind((int)n);

        for (int i = 0; i < m; i++) {
            int u = in.nextInt();
            int v = in.nextInt();

            unionFind.union(u , v);
        }

       int rootN = unionFind.find((int)n);
        long[] sTR = new long[(int) (n+1)];
        Arrays.fill(sTR,Long.MAX_VALUE);
        sTR[1] = 0;
        for (int i = 0; i <= n; i++) {
            if(unionFind.find(i) == rootN){
                sTR[i] = Math.min(sTR[i],sTR[1] + Math.abs(i -1));
            }
        }

        long res = sTR[(int)n];
        if(res == Long.MAX_VALUE){
            res = n - 1;
        }
        System.out.println(res);



    }
}
